package com.definesys.smecrailtransit.module.TotalTime.PopWindowAdapter;

import android.animation.ValueAnimator;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.LayoutDateTimeItemBinding;
import com.definesys.smecrailtransit.databinding.LayoutPopNullDataBinding;
import com.definesys.smecrailtransit.module.TotalTime.bean.DateTimeBean;

import java.util.ArrayList;

/**
 * Created by cjy on 2017/8/29.
 */

public class PopDateTimeAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {
    private static int DATA_NULL = -1;
    private static int DATA_ALL = 1;
    private ArrayList<DateTimeBean> list;
    private int maxNumber;
    private DisplayMetrics dm;
    public boolean showAnimation;
    public int start;

    public PopDateTimeAdapter(DisplayMetrics dm){
        this.list = new ArrayList<>();
        this.dm = dm;
        this.showAnimation = true;
        this.start = 0;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == DATA_ALL){
            LayoutDateTimeItemBinding layoutDateTimeItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    , R.layout.layout_date_time_item,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutDateTimeItemBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutDateTimeItemBinding);
            return smecBaseRecyclerViewHolder;
        }else{
            LayoutPopNullDataBinding layoutPopNullDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    , R.layout.layout_pop_null_data,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutPopNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutPopNullDataBinding);
            return smecBaseRecyclerViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        if(holder.getViewType() == DATA_ALL){
            LayoutDateTimeItemBinding viewDataBinding=(LayoutDateTimeItemBinding)holder.getBinding();
            viewDataBinding.text.setText(this.list.get(position).getDate());
            viewDataBinding.number.setText(String.valueOf(list.get(position).getValue())+"小时");

            int width = this.dm.widthPixels*1/3*this.list.get(position).getValue()/this.maxNumber;
            if(maxNumber != 0){
                if(showAnimation){
                    //属性动画对象
                    ValueAnimator va ;
                    //显示view，高度从0变到height值
                    va = ValueAnimator.ofInt(0,width);
                    va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator valueAnimator) {
                            //获取当前的height值
                            int w =(Integer)valueAnimator.getAnimatedValue();
                            //动态更新view的高度
                            viewDataBinding.pillar.getLayoutParams().width = w;
                            viewDataBinding.pillar.requestLayout();
                        }
                    });
                    va.setDuration(800);
                    //开始动画
                    va.start();
                }else{
                    viewDataBinding.pillar.getLayoutParams().width = width;
                }

            }else{
                viewDataBinding.pillar.getLayoutParams().width = 0;
            }

            viewDataBinding.invalidateAll();
        }
    }

    @Override
    public int getItemCount() {
        if(start == 1){
            if(this.list.size() == 0){
                return 1;
            }else{
                return this.list.size();
            }
        }else{
            return this.list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(this.list.size() == 0){
            return DATA_NULL;
        }else{
            return DATA_ALL;
        }
    }

    public ArrayList<DateTimeBean> getList() {
        return list;
    }

    public void setList(ArrayList<DateTimeBean> list) {
        if(list != null && list.size() != 0){
            this.list = list;
            this.maxNumber = this.list.get(0).getValue();
            for(int i=1 ;i < this.list.size(); i++){
                if(maxNumber < this.list.get(i).getValue()){
                    maxNumber = this.list.get(i).getValue();
                }
            }
        }else{
            this.list = new ArrayList<>();
        }

    }


}
